package input;

import gameStates.absGamesStates.gui.AbsChatState;

import com.jme.input.KeyInput;
import com.jme.input.controls.GameControl;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.controller.ActionChangeController;
import com.jme.input.controls.controller.ControlChangeListener;
import com.jme.scene.Node;

public class ChatKeyInput extends NodeInputHandler {
	
	private GameControl submitChatText;
	private AbsChatState chatState;
	
	public ChatKeyInput(Node rootNode, AbsChatState chateState) {
		super(rootNode);
		this.chatState = chateState;
		
		initInput();
	}
	
	private void initInput() {
		submitChatText = manager.addControl("Submit Chat Text");
		submitChatText.addBinding(new KeyboardBinding(KeyInput.KEY_RETURN));
		
		ActionChangeController submitChat = new ActionChangeController(submitChatText, new ControlChangeListener() {
			@Override
			public void changed(GameControl control, float oldValue, float newValue, float time) {
				if(newValue == 1f && chatIsActive()) chatState.submitChatText();
			}
		});
		rootNode.addController(submitChat);
	}
	
	private boolean chatIsActive() { return chatState.isActive(); }
}